/*  FSM template example - A preprocessor template to construct finite state machines

    Copyright (C) 2013 Dushara Jayasinghe.

    fsm_template is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    fsm_template is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with fsm_template.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <calc.h>

/*
 * The main entry point
 * 
 * Provides a way to test the state machine. It scan the command line
 * arguments for valid characters and calls the appropriate interfaces
 * of the calculator. The following characters are accepted:
 * 
 * 0-9+-/*.
 * c C key
 * d CE key
 * 
 */
int main(int argc, char* argv[])
{
    if( argc > 1)
    {
        int i;
        for( i=1; i<argc; i++)
        {
            const char* key;
            for(key=argv[i]; *key; key++)
            {
                switch(*key)
                {
                    case '0':
                    on_zero();
                    break;
                    
                    case '1':
                    case '2':
                    case '3':
                    case '4':
                    case '5':
                    case '6':
                    case '7':
                    case '8':
                    case '9':
                    on_digit_1_9(*key - '0');
                    break;
                    
                    case '.':
                    on_point();
                    break;
                    
                    case '+':
                    on_add();
                    break;
                    
                    case '-':
                    on_subtract();
                    break;
                    
                    case '*':
                    on_multiply();
                    break;
                    
                    case '/':
                    on_divide();
                    break;
                    
                    case 'c':
                    on_clear_all();
                    break;
                    
                    case 'd':
                    on_clear_entry();
                    break;
                    
                    case '=':
                    on_equals();
                    break;
                    
                    default:
                    printf("unexpected %c\r\n", *key);
                    break;
                }
            }
        }
    }
    
    return 0;
}
